#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>

/*************************************************************************
Copyright @ tiny
file        :led.c
author      :WuChengze
version     :1.0.0
description :led driver file
other       :none
log         :First vesion
***************************************************************************/
#define     LED_MAJOR   200
#define     LED_NAME    "led"

#define     LED_ON      1
#define     LED_OFF     0

/* GPIO register physical address */
#define     CCM_CCGR1_BASE              (0x20C406C)

#define     SW_MUX_GPIO5_IO03_BASE      (0x20E0028)
#define     SW_PAD_GPIO5_IO03_BASE      (0x20E02B4)

#define     GPIO5_DR_BASE               (0x20AC000)
#define     GPIO5_GDIR_BASE             (0x20AC004)

/* remap GPIO register virtual address */
static void __iomem *IMX6U_CCM_CCGR1;
static void __iomem *SW_MUX_GPIO5_IO03;
static void __iomem *SW_PAD_GPIO5_IO03;
static void __iomem *GPIO5_DR;
static void __iomem *GPIO5_GDIR;

void led_switch(u8 sta)
{
    u32 val = 0;
    if(sta == LED_ON){
        val = readl(GPIO5_DR);
        val &= ~(1<<3);
        writel(val, GPIO5_DR);
    }else if(sta == LED_OFF){
        val = readl(GPIO5_DR);
        val |= (1<<3);
        writel(val, GPIO5_DR);
    }
}

static int led_open(struct inode *inode, struct file *filp)
{
    return 0;
}

static ssize_t led_read(struct file *filp, char __user *buf,
                        size_t cnt, loff_t *offt)
{
    return 0;
}

static ssize_t led_write(struct file *filp, const char __user *buf,
                        size_t cnt, loff_t *offt)
{
    int retvalue;
    unsigned char databuf[1];
    unsigned char ledstat;

    retvalue = copy_from_user(databuf, buf, cnt);
    if(retvalue < 0){
        printk("kernel write failed!\n");
    }

    ledstat = databuf[0];

    if(ledstat == LED_ON){
        led_switch(LED_ON);
    }else if(ledstat == LED_OFF){
        led_switch(LED_OFF);
    }
    return 0;
}

static int led_release(struct inode *inode, struct file *filp)
{
    return 0;
}

static struct file_operations led_ops = {
    .owner = THIS_MODULE,
    .open = led_open,
    .read = led_read,
    .write = led_write,
    .release = led_release,
};

static int __init led_init(void){
    int retvalue = 0;
    u32 val = 0;

    /* init led driver */
    /* 1.remap led gpio register */
    IMX6U_CCM_CCGR1 = ioremap(CCM_CCGR1_BASE, 4);
    SW_MUX_GPIO5_IO03 = ioremap(SW_MUX_GPIO5_IO03_BASE, 4);
    SW_PAD_GPIO5_IO03 = ioremap(SW_PAD_GPIO5_IO03_BASE, 4);
    GPIO5_DR = ioremap(GPIO5_DR_BASE, 4);
    GPIO5_GDIR = ioremap(GPIO5_GDIR_BASE, 4);

    /* 2.enable giop5 clock */
    val = readl(IMX6U_CCM_CCGR1);
    val &= ~(3<<30);
    val |= (3<<30);
    writel(val, IMX6U_CCM_CCGR1);

    /* 3.set gpio mux function */
    writel(5, SW_MUX_GPIO5_IO03);

    /* set gpip5_io03 pad attr */
    writel(0x10b0, SW_PAD_GPIO5_IO03);

    /* 4.set gpio5_io03 as output pin */
    val = readl(GPIO5_GDIR);
    val &= ~(1<<3);
    val |= (1<<3);
    writel(val, GPIO5_GDIR);

    /* 5.default set led off */
    val = readl(GPIO5_DR);
    val |= (1<<3);
    writel(val, GPIO5_DR);

    /* 6.register chrdev driver */
    retvalue = register_chrdev(LED_MAJOR, LED_NAME, &led_ops);
    if(retvalue < 0){
        printk("register chrdev failed!\n");
        return -EIO;
    }
    return 0;
}

static void __exit led_exit(void)
{
    /* unmap gpio register */
    iounmap(IMX6U_CCM_CCGR1);
    iounmap(SW_MUX_GPIO5_IO03);
    iounmap(SW_PAD_GPIO5_IO03);
    iounmap(GPIO5_GDIR);
    iounmap(GPIO5_DR);

    /* unregister chrdev driver */
    unregister_chrdev(LED_MAJOR, LED_NAME);
}

module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Tiny");